package pl.org.lutra.planner.gui.main;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.text.InternationalFormatter;

import pl.org.lutra.planner.Planner;
import pl.org.lutra.planner.gui.resources.Text;
import pl.org.lutra.planner.gui.table.DecoTable;
import pl.org.lutra.planner.gui.table.RuntimeTable;
import pl.org.lutra.planner.gui.util.GuiUtil;
import pl.org.lutra.planner.gui.util.TimeFormat;
import pl.org.lutra.planner.model.ModelEvent;
import pl.org.lutra.planner.model.ModelListener;
import pl.org.lutra.planner.model.data.Plan;

public class ResultPanel extends JPanel implements ModelListener {
    private static final long serialVersionUID = -7023570707381261364L;
    
    private Planner planner = Planner.getInstance();
    
    private List<JComponent> toEnableComponents = new ArrayList<JComponent>();
    
    private DecoTable decoTable = new DecoTable();
    private RuntimeTable runtimeTable = new RuntimeTable();
    
    private JFormattedTextField totalTime = new JFormattedTextField(new InternationalFormatter(
            new TimeFormat()));
    
    public ResultPanel() {
        super(new BorderLayout());
        
        planner.getInput().addModelListener(this);
        
        init();
    }
    
    private void init() {
        initTables();
        
        JLabel totalTimeLabel = new JLabel(Text.get("dive.totalTime"));
        toEnableComponents.add(totalTimeLabel);
        
        totalTime.setColumns(6);
        totalTime.setEditable(false);
        totalTime.setHorizontalAlignment(SwingConstants.RIGHT);
        toEnableComponents.add(totalTime);
        
        JPanel additionalPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
        add(additionalPanel, BorderLayout.SOUTH);
        
        additionalPanel.add(totalTimeLabel);
        additionalPanel.add(totalTime);
    }
    
    private void initTables() {
        JPanel tables = new JPanel(new GridBagLayout());
        
        JScrollPane decoScroll = new JScrollPane(decoTable);
        GuiUtil.setTitleBorderWithPadding(decoScroll, "deco.profile");
        toEnableComponents.add(decoTable);
        
        JScrollPane runtimeScroll = new JScrollPane(runtimeTable);
        GuiUtil.setTitleBorderWithPadding(runtimeScroll, "runtime.profile");
        toEnableComponents.add(runtimeTable);
        
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 0.75;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;
        
        tables.add(decoScroll, c);
        
        c = new GridBagConstraints();
        c.gridx = 1;
        c.weightx = 0.25;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;
        
        tables.add(runtimeScroll, c);
        
        add(tables, BorderLayout.CENTER);
    }
    
    @Override
    public void inputHasChanged(ModelEvent event) {
        setEnabled(false);
    }
    
    @Override
    public void calculationFinished(ModelEvent event) {
        Plan plan = planner.getResult();
        
        decoTable.getModel().setSegments(plan.getDeco());
        runtimeTable.getModel().setSegments(plan.getRuntime());
        totalTime.setValue(plan.getTotalTime());
        
        setEnabled(true);
    }
    
    @Override
    public Component add(Component comp) {
        return super.add(comp);
    }
    
    @SuppressWarnings("deprecation")
    @Override
    public void setEnabled(boolean enabled) {
        boolean oldEnabled = isEnabled();
        enable(enabled);
        firePropertyChange("enabled", oldEnabled, enabled);
        if (enabled != oldEnabled) {
            for (JComponent component : toEnableComponents) {
                component.enable(enabled);
                component.firePropertyChange("enabled", oldEnabled, enabled);
            }
            
            repaint();
        }
    }
    
}
